package cn.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ExceResult;
import cn.common.GridCommon;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.sys.RoleEntity;
import cn.kpi.entity.sys.RoleMenuEntity;
import cn.kpi.entity.sys.RoleOrgEntity;
import cn.kpi.entity.sys.UserEntity;
import cn.kpi.service.sys.IRightsService;
import cn.kpi.service.sys.IRoleService;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

	@Autowired
	private IRoleService iRoleService;
	
	@Autowired
	private IRightsService iRightsService;

	@RequestMapping(value = "/role_list")
	public String RoleList(Model model) {
		UserEntity entity=this.GetLoginUser();
		
		model.addAttribute("loginroleid", entity.getRoleid());
		model.addAttribute("usertype", entity.getUsertype());
		
		return "sys/role_list";
	}
	
	@RequestMapping(value = "/role_rights")
	public String RoleRights(Model model,Long roleid) {
		model.addAttribute("roleid", roleid);
		return "sys/role_rights";
	}

	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public LayTable<RoleEntity> GetList(HttpServletRequest request) {

		GridCommon<RoleEntity> countlist = new GridCommon<RoleEntity>();
		List<RoleEntity> lst=null;
		LayTable<RoleEntity> layTable=new LayTable<RoleEntity>();
		try {
			int page=Integer.parseInt(request.getParameter("page"));
			int limit=Integer.parseInt(request.getParameter("limit"));
			
			String RoleName = request.getParameter("rolename");
			System.out.println(RoleName);
			String where = "1=1 ";
			if (RoleName != null && RoleName.length() > 0) {
				where += " AND rolename like '%" + RoleName + "%'";
			}

			int beginrow=(page-1)*limit;
			int endrow=limit;
			String order=" createtime DESC ";
			lst = iRoleService.GetRoleListLimit(where, order, beginrow, endrow);
			for (int i = 0; i < lst.size(); i++) {
				lst.get(i).setRowNum(i + 1);
			}

			// countlist.setCountRecord();
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(lst);
			countlist.setStatus(0);
			countlist.setMsg("");
			
			layTable.setCode(0);
			layTable.setCount(iRoleService.GetRoleListCount(where));
			layTable.setData(lst);
			
		} catch (Exception e) {
			countlist.setCurrentPage(1);
			countlist.setPageSize(10);
			countlist.setItems(null);
			countlist.setStatus(1);
			countlist.setMsg("操作成功");
		}
		// return JsonMapper.toJsonString(countlist);
		//return countlist;
		return layTable;
	}

	
	@RequestMapping(value = "/add_role")
	public String AddRole(HttpServletRequest request,HttpServletResponse response){
		try{
			RoleEntity entity=new RoleEntity();
			entity.setRoleid(Long.parseLong(request.getParameter("roleid")));
			entity.setRemark(request.getParameter("remark"));
			entity.setRolename(request.getParameter("rolename"));
			if(request.getParameter("roletype")!=null&&request.getParameter("roletype").length()>0)
			{
				entity.setRoletype(Integer.parseInt(request.getParameter("roletype")));
			}
			
			int iResult=0;
			if(entity.getRoleid()>0){
				iResult=iRoleService.UpdateEntity(entity);
			}
			else{
				iResult=iRoleService.InsertEntity(entity);	
			}
			
			return renderStringScript(response,"<script>window.parent.CloseDialog("+iResult+")</script>");
		}
		catch(Exception ex){
			return renderStringScript(response,"<script>window.parent.CloseDialog(0)</script>");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete_role")
	public ExceResult DeleteRole(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long roleid=Long.parseLong(request.getParameter("roleid"));
		 int iResult=iRoleService.DeleteEntity(roleid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/get_menu_list")
	public List<RoleMenuEntity> GetMenuList(HttpServletRequest request,Long roleid ) {
		List<RoleMenuEntity> lst=null;
		try {
			lst = iRightsService.GetRoleMenuList(roleid);
			for(int i=0;i<lst.size();i++){
				if(lst.get(i).getIschecked()==1){
					lst.get(i).setLay_CHECKED(true);
				}else{
					lst.get(i).setLay_CHECKED(false);
				}
			}

			return lst;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}
	
	@ResponseBody
	@RequestMapping(value = "/insert_rolemenu")
	public ExceResult InsertRoleMenu(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long roleid=Long.parseLong(request.getParameter("roleid"));
		 String strmenuids=request.getParameter("menuids");
		 String[] menuidArr=strmenuids.split(",");
		 
		 List<Long> list=new ArrayList<Long>();
		 for(int i=0;i<menuidArr.length;i++){
			 list.add(Long.parseLong(menuidArr[i]));
		 }
		 
		 int iResult=iRightsService.InsertRoleMenu(list,roleid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_org_list")
	public List<RoleOrgEntity> GetOrgList(HttpServletRequest request) {
		List<RoleOrgEntity> lst=null;
		try {
			Long roleid = Long.parseLong(request.getParameter("roleid"));
			
			lst = iRightsService.GetRoleOrgList(roleid);
			for(int i=0;i<lst.size();i++){
				if(lst.get(i).getIschecked()==1){
					lst.get(i).setLay_CHECKED(true);
				}else{
					lst.get(i).setLay_CHECKED(false);
				}
			}

			return lst;			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}
	
	@ResponseBody
	@RequestMapping(value = "/insert_roleorg")
	public ExceResult InsertRoleOrg(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long roleid=Long.parseLong(request.getParameter("roleid"));
		 String strorgids=request.getParameter("orgids");
		 String[] orgidArr=strorgids.split(",");
		 
		 List<Long> list=new ArrayList<Long>();
		 for(int i=0;i<orgidArr.length;i++){
			 list.add(Long.parseLong(orgidArr[i]));
		 }
		 
		 int iResult=iRightsService.InsertRoleOrg(list,roleid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
}
